<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="中文名" prop="chineseName">
        <el-input v-model="queryParams.chineseName" placeholder="请输入中文名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="英文名" prop="englishName">
        <el-input v-model="queryParams.englishName" placeholder="请输入英文名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="出生日期" prop="birthdate">
        <el-input v-model="queryParams.birthdate" placeholder="请输入出生日期" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="whatsapp" prop="whatsapp">
        <el-input v-model="queryParams.whatsapp" placeholder="请输入whatsapp" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="请选择性别">
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="学历" prop="education">
        <el-input v-model="queryParams.education" placeholder="请输入学历" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="服务范围" prop="serve">
        <el-input v-model="queryParams.serve" placeholder="请输入服务范围" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="其他服务" prop="otherServe">
        <el-input v-model="queryParams.otherServe" placeholder="请输入其他服务" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="不确定时间" prop="confirmServeTime">
        <el-input v-model="queryParams.confirmServeTime" placeholder="请输入不确定时间" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="服务时间" prop="serveTime">
        <el-input v-model="queryParams.serveTime" placeholder="请输入服务时间" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="紧急联络人" prop="emergencyContact">
        <el-input v-model="queryParams.emergencyContact" placeholder="请输入紧急联络人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="紧急联络人电话" prop="emergencyContactMobile">
        <el-input v-model="queryParams.emergencyContactMobile" placeholder="请输入紧急联络人电话" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['fund:volunteer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['fund:volunteer:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['fund:volunteer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['fund:volunteer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="volunteerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="中文名" align="center" prop="chineseName" />
      <el-table-column label="英文名" align="center" prop="englishName" />
      <!-- <el-table-column label="出生日期" align="center" prop="birthdate" /> -->
      <el-table-column label="whatsapp" align="center" prop="whatsapp" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <span>{{ scope.row.gender == '1' ? '男' : '女' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" />
      <!-- <el-table-column label="通讯地址" align="center" prop="contactAddress" /> -->
      <!-- <el-table-column label="学历" align="center" prop="education" /> -->
      <!-- <el-table-column label="服务范围" align="center" prop="serve" /> -->
      <!-- <el-table-column label="其他服务" align="center" prop="otherServe" /> -->
      <el-table-column label="不确定时间" align="center" prop="confirmServeTime">
        <template slot-scope="scope">
          <span>{{ confirmTimeEmun[scope.row.confirmServeTime] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="服务时间" align="center" prop="serveTime" /> -->
      <el-table-column label="语言" align="center" prop="language">
        <template slot-scope="scope">
          <!-- <span>{{ dict.type.sys_volunteer_lang[scope.row.language - 1].value == 4 ? scope.row.otherLanguage :
            dict.type.sys_volunteer_lang[scope.row.language - 1].label }}</span> -->
          <span>{{ JSON.parse(scope.row.language).map((item,index)=>dict.type.sys_volunteer_lang[index].label).toString()}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="紧急联络人" align="center" prop="emergencyContact" />
      <el-table-column label="紧急联络人电话" align="center" prop="emergencyContactMobile" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ statusEmun[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="mtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mtime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-s-order" @click="handleUpdate(scope.row)"
            v-hasPermi="['fund:volunteer:edit']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fund:volunteer:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit"
      @pagination="getList" />
  </div>
</template>

<script>
import { listVolunteer, delVolunteer, addVolunteer, updateVolunteer } from "@/api/fund/volunteer";
export default {
  name: "Volunteer",
  dicts: ['sys_lang', 'sys_user_sex', 'sys_volunteer_lang'],
  data() {
    
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 义工表格数据
      volunteerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        chineseName: null,
        englishName: null,
        birthdate: null,
        whatsapp: null,
        gender: null,
        mobile: null,
        contactAddress: null,
        education: null,
        serve: null,
        otherServe: null,
        confirmServeTime: null,
        serveTime: null,
        language: null,
        emergencyContact: null,
        emergencyContactMobile: null,
        status: null,
        ctime: null,
        mtime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        chineseName: [
          { required: true, message: "中文名不能为空", trigger: "blur" }
        ],
        englishName: [
          { required: true, message: "英文名不能为空", trigger: "blur" },
        ],
        birthdate: [
          { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        whatsapp: [
          { required: true, message: "whatsapp不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        contactAddress: [
          { required: true, message: "通讯地址不能为空", trigger: "blur" }
        ],
        education: [
          { required: true, message: "学历不能为空", trigger: "blur" }
        ],
        serve: [
          { required: true, message: "服务范围不能为空", trigger: "blur" }
        ],
        otherServe: [
          { required: true, message: "其他服务不能为空", trigger: "blur" }
        ],
        confirmServeTime: [
          { required: true, message: "不确定时间不能为空", trigger: "blur" }
        ],
        serveTime: [
          { required: true, message: "服务时间不能为空", trigger: "blur" }
        ],
        language: [
          { required: true, message: "语言不能为空", trigger: "blur" }
        ],
        emergencyContact: [
          { required: true, message: "紧急联络人不能为空", trigger: "blur" }
        ],
        emergencyContactMobile: [
          { required: true, message: "紧急联络人电话不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        ctime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      },
      statusEmun: {
        0: '有效',
        1: '无效'
      },
      confirmTimeEmun: {
        '1': '是',
        '0': '否',
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询义工列表 */
    getList() {
      this.loading = true;
      listVolunteer(this.queryParams).then(response => {
        this.volunteerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chineseName: null,
        englishName: null,
        birthdate: null,
        whatsapp: null,
        gender: null,
        mobile: null,
        contactAddress: null,
        education: null,
        serve: null,
        otherServe: null,
        confirmServeTime: null,
        serveTime: null,
        language: null,
        emergencyContact: null,
        emergencyContactMobile: null,
        status: null,
        ctime: null,
        createBy: null,
        mtime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加义工";
      this.$router.push({
        path: '/fund/actionVolunteer',
        query: {
          type: 1,
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: '/fund/actionVolunteer',
        query: {
          type: 2,
          id: row.id || this.ids
        }
      })
      // this.reset();
      // const id = row.id || this.ids
      // getVolunteer(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改义工";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVolunteer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVolunteer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除义工编号为"' + ids + '"的数据项？').then(function () {
        return delVolunteer(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/volunteer/export', {
        ...this.queryParams
      }, `volunteer_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
